package com.example.mall.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.mall.constants.BaseConstants;
import com.example.mall.domain.TimeLimitedDiscounts;
import com.example.mall.dto.TimeLimitedDiscountsDto;
import com.example.mall.mapper.TimeLimitedDiscountsMapper;
import com.example.mall.query.PageDTO;
import com.example.mall.query.TimeLimitedDiscountsQuery;
import com.example.mall.service.ITimeLimitedDiscountsService;

import com.example.mall.util.AssertUtil;
import com.example.mall.util.CodeGenerateUtils;
import com.example.mall.util.CopyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 限时折扣表 服务实现类
 * </p>
 *
 * @author whale
 * @since 2025-05-06
 */
@Service
public class TimeLimitedDiscountsServiceImpl extends ServiceImpl<TimeLimitedDiscountsMapper, TimeLimitedDiscounts> implements ITimeLimitedDiscountsService {

    @Autowired
    private TimeLimitedDiscountsMapper timeLimitedDiscountsMapper;

    @Override
    public PageDTO<TimeLimitedDiscountsDto> queryPage(TimeLimitedDiscountsQuery timeLimitedDiscountsQuery) {
        if (timeLimitedDiscountsQuery == null) {
            timeLimitedDiscountsQuery = new TimeLimitedDiscountsQuery();
        }
        Page<TimeLimitedDiscounts> page = timeLimitedDiscountsQuery.toMpPageByDefaultCreateTimeAsc();
        Page<TimeLimitedDiscounts> cartPage = lambdaQuery()
                .like(timeLimitedDiscountsQuery.getDiscountId()!= null, TimeLimitedDiscounts::getDiscountId, timeLimitedDiscountsQuery.getDiscountId())
                .like(timeLimitedDiscountsQuery.getProductId()!= null, TimeLimitedDiscounts::getProductId, timeLimitedDiscountsQuery.getProductId())
                .like(timeLimitedDiscountsQuery.getDiscountPrice()!= null, TimeLimitedDiscounts::getDiscountPrice, timeLimitedDiscountsQuery.getDiscountPrice())
                .like(timeLimitedDiscountsQuery.getStatus()!= null, TimeLimitedDiscounts::getStatus, timeLimitedDiscountsQuery.getStatus())
                .apply(timeLimitedDiscountsQuery.getStartTime() != null,
                        "DATE_FORMAT(start_time, '%Y-%m-%d') = DATE_FORMAT({0}, '%Y-%m-%d')",
                        timeLimitedDiscountsQuery.getStartTime())
                .apply(timeLimitedDiscountsQuery.getEndTime() != null,
                        "DATE_FORMAT(end_time, '%Y-%m-%d') = DATE_FORMAT({0}, '%Y-%m-%d')",
                        timeLimitedDiscountsQuery.getEndTime())
                .orderByDesc(TimeLimitedDiscounts::getUpdateDate)
                .page(page);
        return PageDTO.of(cartPage, TimeLimitedDiscountsDto.class);
    }

    @Override
    public void insert(TimeLimitedDiscountsDto timeLimitedDiscountsDto) {
        TimeLimitedDiscounts timeLimitedDiscounts = new TimeLimitedDiscounts();
        CopyUtils.copyProperties(timeLimitedDiscountsDto, timeLimitedDiscounts);
        timeLimitedDiscounts.setDiscountId(CodeGenerateUtils.generateUniqueId());
        timeLimitedDiscounts.setCreateDate(new Date());
        timeLimitedDiscounts.setUpdateDate(new Date());
        timeLimitedDiscounts.setStatus(String.valueOf(BaseConstants.NOT_USED));
        timeLimitedDiscountsMapper.insert(timeLimitedDiscounts);
    }

    @Override
    public void update(TimeLimitedDiscountsDto timeLimitedDiscountsDto) {
        AssertUtil.isNotNull(timeLimitedDiscountsDto.getDiscountId(), "discountId不能为空");
        TimeLimitedDiscounts timeLimitedDiscounts = timeLimitedDiscountsMapper.selectById(timeLimitedDiscountsDto.getDiscountId());
        AssertUtil.isNotNull(timeLimitedDiscounts, "限时折扣不存在");
        CopyUtils.copyProperties(timeLimitedDiscountsDto, timeLimitedDiscounts);
        timeLimitedDiscounts.setUpdateDate(new Date());
        timeLimitedDiscountsMapper.updateById(timeLimitedDiscounts);
    }

    @Override
    public void delete(Long discountId) {
        AssertUtil.isNotNull(discountId, "discountId不能为空");
        TimeLimitedDiscounts timeLimitedDiscounts = timeLimitedDiscountsMapper.selectById(discountId);
        AssertUtil.isNotNull(timeLimitedDiscounts, "限时折扣不存在");
        timeLimitedDiscounts.setStatus(String.valueOf(BaseConstants.EXPIRED));
        timeLimitedDiscounts.setUpdateDate(new Date());
        timeLimitedDiscountsMapper.updateById(timeLimitedDiscounts);
    }

    @Override
    public void deleteBatch(List<Long> discountIds) {
        AssertUtil.isNotNull(discountIds, "discountIds不能为空");
        List<TimeLimitedDiscounts> timeLimitedDiscounts = timeLimitedDiscountsMapper.selectBatchIds(discountIds);
        AssertUtil.isNotNull(timeLimitedDiscounts, "限时折扣不存在");
        for (TimeLimitedDiscounts discount : timeLimitedDiscounts) {
            discount.setStatus(String.valueOf(BaseConstants.EXPIRED));
            discount.setUpdateDate(new Date());
            timeLimitedDiscountsMapper.updateById(discount);
        }
    }

    @Override
    public List<TimeLimitedDiscounts> queryTimeLimitedDiscountsList() {
        return this.list();
    }

    @Override
    public TimeLimitedDiscounts queryTimeLimitedDiscountsById(Long discountId) {
        AssertUtil.isNotNull(discountId, "discountId不能为空");
        return timeLimitedDiscountsMapper.selectById(discountId);
    }
}
